package com.vken.blog.action;

import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import com.vken.blog.model.Category;
import com.vken.blog.model.Entry;
import com.vken.blog.service.BlogManager;
import com.vken.blog.service.CategoryManager;
import com.vken.blog.service.EntryManager;
import com.vken.blog.util.Const;
import com.vken.blog.util.Pagination;

public class EntryAction extends ActionSupport {
	private static final long serialVersionUID = 7376975876247738769L;
	private Integer p;
	private Integer id;
	private Entry entry;
	private Pagination pagination;
	private List<Category> categorys;
	private EntryManager entryManager;
	private CategoryManager categoryManager;
	private BlogManager blogManager;

	public void setEntryManager(EntryManager entryManager) {
		this.entryManager = entryManager;
	}

	public void setCategoryManager(CategoryManager categoryManager) {
		this.categoryManager = categoryManager;
	}

	public void setBlogManager(BlogManager blogManager) {
		this.blogManager = blogManager;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public void setP(Integer p) {
		this.p = p;
	}

	public Entry getEntry() {
		return entry;
	}

	public void setEntry(Entry entry) {
		this.entry = entry;
	}

	public List<Category> getCategorys() {
		return categorys;
	}

	public List<Category> setCategorys() {
		return categorys;
	}

	public Pagination getPagination() {
		return pagination;
	}

	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}

	public String prepare() {
		categorys = categoryManager.list(Const.BLOG_ID);
		return SUCCESS;
	}

	public String add() {
		if (entry.getCategory().getId() != null) {
			entryManager.insert(entry, entry.getCategory().getId());
		}
		return SUCCESS;
	}

	public String load() {
		if (id == null) {
			return INPUT;
		}
		entry = entryManager.get(id);
		categorys = categoryManager.list(Const.BLOG_ID);
		return SUCCESS;
	}

	public String list() {
		if (p == null || p <= 0) {
			p = new Integer(1);
		}
		Integer pageSize = blogManager.get(Const.BLOG_ID).getBackendPageSize();
		pagination = entryManager.getListByBlogId(Const.BLOG_ID, null, pageSize, p);
		return SUCCESS;
	}

	public String update() {
		entryManager.update(entry);
		return SUCCESS;
	}

	public String delete() {
		if (id != null) {
			entryManager.delete(id);
		}
		return SUCCESS;
	}
}
